package com.ruoyi.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.business.domain.ShopVerification;
import com.ruoyi.business.mapper.ShopVerificationMapper;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.ShopMapper;
import com.ruoyi.business.domain.Shop;
import com.ruoyi.business.service.IShopService;

/**
 * 店铺Service业务层处理
 *
 * @author zsq
 * @date 2024-09-11
 */
@Service
public class ShopServiceImpl implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopVerificationMapper shopVerificationMapper;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 查询店铺
     *
     * @param id 店铺主键
     * @return 店铺
     */
    @Override
    public Shop selectShopById(Long id) {
        return shopMapper.selectShopById(id);
    }

    /**
     * 查询店铺列表
     *
     * @param shop 店铺
     * @return 店铺
     */
    @Override
    public List<Shop> selectShopList(Shop shop) {
        return shopMapper.selectShopList(shop);
    }

    /**
     * 新增店铺
     *
     * @param shop 店铺
     * @return 结果
     */
    @Override
    public int insertShop(Shop shop) {
        return shopMapper.insertShop(shop);
    }

    /**
     * 修改店铺
     *
     * @param shop 店铺
     * @return 结果
     */
    @Override
    public int updateShop(Shop shop) {
        shop.setUpdateTime(DateUtils.getNowDate());
        return shopMapper.updateShop(shop);
    }

    /**
     * 批量删除店铺
     *
     * @param ids 需要删除的店铺主键
     * @return 结果
     */
    @Override
    public int deleteShopByIds(Long[] ids) {
        return shopMapper.deleteShopByIds(ids);
    }

    /**
     * 删除店铺信息
     *
     * @param id 店铺主键
     * @return 结果
     */
    @Override
    public int deleteShopById(Long id) {
        return shopMapper.deleteShopById(id);
    }

    @Override
    public List<Shop> selectShopServiceById(Long id) {
        return shopMapper.selectShopAllById(id);
    }

    @Override
    public int checkShop(Shop shop) {
        if (shop.getAuditStatusId() == 2) {
            //找到注册手机号
            ShopVerification shopVerification = shopVerificationMapper.selectOne(new QueryWrapper<ShopVerification>().eq("mid", shop.getId()));
            // 注册用户信息
            SysUser sysUser = new SysUser();
            sysUser.setUserName(shopVerification.getMobile());
            sysUser.setNickName(shopVerification.getMobile());
            sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
            R<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
            if (R.FAIL == registerResult.getCode()) {
                throw new ServiceException(registerResult.getMsg());
            }
            //存入redis当中：两个key，value分别是店铺信息和店铺位置
            redisService.setCacheObject("shop_id:" + shop.getId(), shop);
            redisService.setGeoCache("shops", new Point(Double.parseDouble(shop.getLongitude()), Double.parseDouble(shop.getLatitude())), shop.getId());
            return shopMapper.updateShop(shop);
        } else if (shop.getAuditStatusId() == 3) {
            //审核驳回，发送短信通知（没做，自定义内容短信得掏钱）
            return shopMapper.updateShop(shop);
        } else {
            return 0;
        }
    }
}
